package com.javasm.sys.impl;

import com.javasm.sys.tree.PosTreeUtil;
import com.javasm.sys.PosService;
import com.javasm.sys.dao.PosDao;
import com.javasm.sys.SysPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class PosServiceImpl implements PosService {

    private static Logger l = LoggerFactory.getLogger(PosServiceImpl.class);

    @Resource
    protected PosDao posDao;

    @Override
    public Integer delPosByIds(List<String> posId) {
        Integer integer = posDao.delPosByIds(posId);
        return integer;
    }

    @Override
    public List<SysPos> selectAll(SysPos sysPos) {
        List<SysPos> list = posDao.selectAll(sysPos);
        return list;
    }



    @Override
    public int  addPos(SysPos sysPos) {

        //如果取出的最大id和当前输入的id一致，说明已经是最低一层
        //新部门编号+01
        //否则加1
        String maxIdStr = posDao.getMaxId(sysPos.getPid());
        Integer maxId = null;
        if (maxIdStr.equals(sysPos.getPid())){
            maxIdStr += "01";
            maxId = Integer.parseInt(maxIdStr);
        }else {
            maxId = Integer.parseInt(maxIdStr)+1;
        }
        sysPos.setPosId(maxId.toString());
        return posDao.addPos(sysPos);

    }

    @Override
    public List<SysPos> getAll() {
        List<SysPos> all = posDao.getAll();
        return all;
    }

    @Override//根据state查询
    public Collection<SysPos> getListByState(String posState) {
        Collection<SysPos> listByState = posDao.getListByState(posState);
        //  List<SysPos> listByState1 = PosTreeUtil.buildTree(listByState, "-1");
        return listByState;
    }


    @Override//获取树形菜单
    public List<SysPos> getList() {
        List<SysPos> list = posDao.getList();
        List<SysPos> list1 = PosTreeUtil.buildTree(list, "-1");

        return list1;
    }


    @Override//根据code模糊查询
    public Collection<SysPos> getListByPosCode(String posCode) {
        Collection<SysPos> listByCode = posDao.getListByPosCode(posCode);

//        List<SysPos> listByCode1 = PosTreeUtil.buildTree(listByCode, "-1");

        return listByCode;
    }

    @Override//根据name模糊查询
    public Collection<SysPos> getListByPosName(String posName) {
        Collection<SysPos> listByPosName = posDao.getListByPosName(posName);
        //  List<SysPos> listByPosName1 = PosTreeUtil.buildTree(listByPosName, "-1");
        return listByPosName;
    }

    @Override
    public Integer delByPosId(String posId) {
        Integer s = posDao.delByPosId(posId);
        return s;
    }

    @Override
    public Integer updatePos(SysPos sysPos) {

        return posDao.updatePos(sysPos);
    }

    @Override
    public Integer updateByIdState(String posState,String posId) {
        Integer integer = posDao.updateByIdState(posState,posId);
        return integer;
    }


}

